﻿using Entity.Models;
using Entity.Models.BaseEntitys;
using Entity.Models.Entities;
using IDAL;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace DAL
{
    public class BaseEntityDAL<T>: IBaseEntity<T> where T: BaseEntity 
    {
        //数据库连接上下文
        RepositorySysContext _repositorySys;
        public BaseEntityDAL(RepositorySysContext repositorySys)
        {
            _repositorySys = repositorySys;
        }
        
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int AddEntity(T entity)
        {
            _repositorySys.Set<T>().Add(entity);
            return _repositorySys.SaveChanges();
        }

        /// <summary>
        /// 获取数据 
        /// </summary>
        /// <returns></returns>
        public DbSet<T> getEntitys()
        {
            return _repositorySys.Set<T>();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int UpdataEntity(T entity)
        {
            _repositorySys.Set<T>().Update(entity);
            return _repositorySys.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int DalateEntity(string Id)
        {
            T entity = _repositorySys.Set<T>().FirstOrDefault(i=>Id.EndsWith(Id));
            if (entity != null)
            {
                _repositorySys.Set<T>().Remove(entity);
            }
            return _repositorySys.SaveChanges();
        }

    }
}
